package com.zb.sannuo.testing.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.sannuo.common.domain.*;
import com.zb.sannuo.common.domain.search.PatientDrugstoreSearch;
import com.zb.sannuo.testing.dao.TestingMapper;
import com.zb.sannuo.testing.service.TestingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

@Service
public class TestingServiceImpl extends ServiceImpl<TestingMapper, SccPatient> implements TestingService {

    @Resource
    private TestingMapper testingMapper;

    @Override
    public Page selectListPage(int current, int number, PatientDrugstoreSearch search) {
        System.out.println(search);
        Page page = new Page(current,number);
        return page.setRecords(testingMapper.query(page,search));
    }

    @Override
    public int updateStatusId(Integer patientId) {
        return testingMapper.updateStatusId(patientId);
    }

    @Transactional(timeout = 5,rollbackFor = Exception.class)
    @Override
    public int addAll(Patient patient, SccUserLogin sccUserLogin, Role role, Drugstore drugstore) {
        patient.setStatus(1);
        sccUserLogin.setPassword("88888");
        role.setRoleName("检测者");
        drugstore.setStoreName("老百姓大药房");
        sccUserLogin.setCustomerCode("88888");
        drugstore.setStoreId(1);
        try {
            testingMapper.addUserLogin(sccUserLogin);
            patient.setPatientCode(sccUserLogin.getUserId());
//            patient.setCustomerCode(sccUserLogin.getUserId());
            patient.setUserId(sccUserLogin.getUserId());
            testingMapper.addPatient(patient);
            patient.setUserId(sccUserLogin.getUserId());
            Integer userId = sccUserLogin.getUserId();
            testingMapper.addUserStore(drugstore.getStoreId(),userId);
            role.setRoleId(1);
            testingMapper.addUserRole(role.getRoleId(),userId);
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
        return 1;
    }
}
